﻿//$('#AddCat').click(function () {
//    var url = "/admin/edittags/create";
//    var name = $("#name").val();
//    var slug = $("#slug").val();
//    var des = $("#des").val();
//    $.post(url, { name: name, slug: slug, des: des }, function (data)
//    {
//        $("#msg").html(data);
//        $("#name").val("");
//        $("#slug").val("");
//    });
//});

function getcategory()
{
    $(document).ready()
    {
        var url = "/admin/edittags/AjxaGetCategory";
        $.ajax({
            url: url,
            ContentType: 'application/html; charset=utf-8',
            type: 'GET',
            dataType: 'html'
        })
         .success(function (result) {
             $('#example_wrapper').html(result);
         })
         .error(function (xhr, status) {
        alert(status);
         })
    }
}

$('#AddCat').click(function ()
    {
    var url = "/admin/edittags/create";
    var name = $("#name").val();
    var slug = $("#slug").val();
    var des = $("#des").val();
    $.ajax({
        url: url,
        ContentType: 'application/html; charset=utf-8',
        data: { name: name, slug: slug, des: des },
        type: 'POST',
        dataType:'html'
    })
    .success(function (result)
    {
        $("#msg").html(result);
        $("#name").val("");
        $("#slug").val("");
        getcategory();
    })
    .error(function(xhr,status)
    {
        alert(status);
    })
    
});

///

var namesTemplate = Handlebars.compile($('#namesTemplate').html());
var pagerTemplate = Handlebars.compile($('#pagerTemplate').html());

var fetchPage = function (pageUrl) {
    $.get(pageUrl, function (data) {
        window.location.hash = data.pager.PageNumber;

        // determine how many pages should be shown
        var start, end;
        var numberOfPagesToShowAtOnce = 10;
        var halfOfPagesToShowAtOnce = Math.floor(numberOfPagesToShowAtOnce / 2);
        start = data.pager.PageNumber - halfOfPagesToShowAtOnce;
        if (start < 1)
            start = 1;
        if ((start + numberOfPagesToShowAtOnce) > data.pager.PageCount)
            end = data.pager.PageCount;
        else
            end = start + numberOfPagesToShowAtOnce;

        // add each visible page to an array to show
        data.pager.Pages = [];
        for (var i = start; i <= end; i++) {
            data.pager.Pages.push({
                PageNumber: i,
                Selected: i === data.pager.PageNumber
            });
        }

        // used for showing/hiding ellipsis
        data.pager.FirstPageIsVisible = start === 1;
        data.pager.LastPageIsVisible = end === data.pager.PageCount;

        // add urls for previous & next pages
        data.pager.PreviousPageNumber = data.pager.PageNumber - 1
        data.pager.NextPageNumber = data.pager.PageNumber + 1

        // render templates
        $('#namesList').html(namesTemplate(data.names)).attr('start', data.pager.FirstItemOnPage);
        $('#namesPager').html(pagerTemplate(data.pager));

        // bind to page click events
        $('#namesPager li a').click(function (event) {
            event.preventDefault();
            var newPageUrl = $(event.target).attr('href');
            if (newPageUrl) {
                fetchPage(newPageUrl);
            }
        });
    });
}

$(function () {
    var page = window.location.hash
        ? window.location.hash.slice(1)
        : 1;
    fetchPage('AjxaGetCategory?page=' + page);
});